package com.xcity.game.combat.builder;

import java.util.ArrayList;
import java.util.List;

import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.game.combat.Combat;
import com.xcity.game.combat.CombatBuilder;
import com.xcity.game.combat.CombatTeam;
import com.xcity.game.combat.listener.CombatListener;
import com.xcity.game.combat.unit.impl.EmployeeCombatUnit;
import com.xcity.game.combat.unit.impl.EmptyCombatUnit;
import com.xcity.game.combat.unit.impl.NpcCombatUnit;
import com.xcity.game.combat.unit.impl.PlayerCombatUnit;
import com.xcity.game.npc.NpcTemplate;

/**
 * @deprecated
 * @see DefaultCombatBuilder
 * @author yang.li
 *
 */
@Deprecated
public class PVECombatBuilder implements CombatBuilder {

	private Player player;
	private List<Employee> employees;
	private List<NpcTemplate> npcs;
	private NpcTemplate bossNpc;
	private CombatListener listener;
	
	public PVECombatBuilder() {}
	
	public PVECombatBuilder setPlayer(Player player) {
		this.player = player;
		return this;
	}
	
	public PVECombatBuilder setEmployees(List<Employee> employees) {
		this.employees = employees;
		return this;
	}
	
	public PVECombatBuilder setNpcs(List<NpcTemplate> npcs) {
		this.npcs = npcs;
		return this;
	}
	
	public PVECombatBuilder addNpc(NpcTemplate npc) {
		if (npcs == null) {
			npcs = new ArrayList<NpcTemplate>();
		}
		npcs.add(npc);
		return this;
	}
	
	public PVECombatBuilder setBossNpc(NpcTemplate bossNpc) {
		this.bossNpc = bossNpc;
		return this;
	}
	
	public PVECombatBuilder setListener(CombatListener listener) {
		this.listener = listener;
		return this;
	}
	
	public Combat build() {
		return new Combat(buildPlayerTeam(), buildNpcTeam(), Combat.Type.PVE, listener);
	}
	
	CombatTeam buildPlayerTeam() {
		CombatTeam team = new CombatTeam();
		// add the leader in the first position
		PlayerCombatUnit leader = new PlayerCombatUnit(team, player);
		team.add(leader);
		// add the members
		for (int i = 0, size = employees != null ? employees.size() : 0; i < size; i++) {
			Employee e = employees.get(i);
			team.add(e != null ? new EmployeeCombatUnit(team, e) : EmptyCombatUnit.INSTANCE);
		}
		fillEmpty(team);
		return team;
	}
	
	CombatTeam buildNpcTeam() {
		CombatTeam team = new CombatTeam();
		// add the leader in the first position
		team.add(bossNpc != null ? new NpcCombatUnit(team, bossNpc, true) : EmptyCombatUnit.INSTANCE);
		// add the members
		for (int i = 0, size = npcs.size(); i < size; i++) {
			NpcTemplate npc = npcs.get(i);
			team.add(npc != null ? new NpcCombatUnit(team, npc) : EmptyCombatUnit.INSTANCE);
		}
		fillEmpty(team);
		return team;
	}
	
	// 填充剩余空位
	private static void fillEmpty(CombatTeam team) {
		int n = CombatTeam.DEFAULT_TEAM_CAPACITY - team.size();
		for (int i = 0; i < n; i++) {
			team.add(EmptyCombatUnit.INSTANCE);
		}
	}
	
}
